export type Skill = number

export type SkillMap<T> = T[]

export type SkillData = {
  name: string
  cost: number
  versus: SkillMap<1 | 0 | -1>
}

export type Skills = SkillData[]

export type Agent = SkillMap<number>[][]

const get = <T>(arr: T[], i: number) =>
  i < arr.length ? arr[i] : arr[arr.length - 1]

const randChoose = (weight: number[]) => {
  let sum = 0, idx = 0
  weight.forEach((w, i) => {
    sum += w
    if (w / sum > Math.random())
      idx = i
  })
  return idx
}

export const play = (agent: Agent, my: number, their: number): Skill =>
  randChoose(get(get(agent, my), their))
